{
    init: function(elevators, floors) {
        console.log('elevators',elevators);
        console.log('floors',floors);

        var floor_req={};

        var findex;
        for(findex=0;findex<floors.length;findex++){
            floor_req[findex]=false;
        }

        var eindex;
        for(eindex=0;eindex<elevators.length;eindex++){
            // Let's use the first elevator



            // Whenever the elevator is idle (has no more queued destinations) ...
            elevators[eindex].on("idle", function() {
                console.log('elevator idle of ',eindex,this)
                // let's go to all the floors (or did we forget one?)
                //this.goToFloor(0);
            });
            elevators[eindex].on("floor_button_pressed", function(floorNum) {
                floor_req[floorNum]=true;
                var next;
                if(this.destinationDirection()==='up'){
                    for(next=this.currentFloor()+1;next<floors.length;next++){
                        if(floor_req[next]){
                            this.goToFloor(next);
                            return;
                        }
                    }
                }
                else if(this.destinationDirection()==='down'){
                    for(next=this.currentFloor()-1;next>=0;next--){
                        if(floor_req[next]){
                            this.goToFloor(next);
                            return;
                        }
                    }
                }
                else {
                    this.goToFloor(floorNum);
                }
            } );
            elevators[eindex].on("stopped_at_floor", function(floorNum) {
                // Maybe decide where to go next?
                floor_req[floorNum]=false;
            });
            elevators[eindex].on("passing_floor", function(floorNum, direction) { 
            });
        }

        var findex;
        for(findex=0;findex<floors.length;findex++){
            //var floor=floors[findex];
            floors[findex].on("up_button_pressed", function() { 
                //elevator.goToFloor(floor.floorNum());
                floor_req[findex]=true;
            });
            floors[findex].on("down_button_pressed", function() {
                //elevator.goToFloor(floor.floorNum());
                floor_req[findex]=true;
            });
        }
    },
    update: function(dt, elevators, floors) {
        // We normally don't need to do anything here
    }
}